package com.kefu.admin.modules.tgqmdj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kefu.admin.modules.comm.controller.AbstractController;
import com.kefu.admin.modules.sys.model.User;
import com.kefu.admin.modules.sys.vo.base.PageResultVo;
import com.kefu.admin.modules.tgqmdj.model.Article;
import com.kefu.admin.modules.tgqmdj.model.TreeMenus;
import com.kefu.admin.modules.tgqmdj.service.impl.ArticleServiceImpl;
import com.kefu.admin.modules.tgqmdj.service.impl.MenuServiceImpl;
import com.kefu.admin.utils.Convert;
import com.kefu.admin.utils.PageUtil;
import com.kefu.admin.utils.ResultUtil;
import com.kefu.admin.utils.Ret;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
/**
 * @Description:
 * @version: v1.0.0
 * @author Hjj
 * @date: 2019年3月11日 下午4:36:17
 */
@Controller
@RequestMapping("/tgqmdj")
public class ArticleController extends AbstractController{
    private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    private ArticleServiceImpl articleServiceImpl;
    @Autowired
    private MenuServiceImpl menuServiceImpl;

 
    @RequestMapping(value={"/lists"})
    public String menulist(HttpServletRequest request){
        return "tgqmdj/lists";
    }
    
    @RequestMapping(value={"/articlelist"})
    public String list(HttpServletRequest request,Model model,Long articleId){
    	Article article = articleServiceImpl.queryArticleByArticleId(articleId);
    	if(null==article){
    		article = new Article();
    		article.setTitle("");
    		article.setTextHtml("");
    		article.setArticleId(0L);
    		article.setMenuId(0L);
    		article.setMenuName("");
    	}
    	article.setMenuName("所属菜单:"+article.getMenuName());
    	model.addAttribute("article", article);
        return "tgqmdj/articlelist";
    }

	/**
	 * 查询当前登录用户所在部门以及子部门
	 * @param userId
	 * @return
	 */
	@RequestMapping(value={"/queryMenu"})
	@ResponseBody
	public Ret queryDeptByUserId(){
		List<TreeMenus> list = articleServiceImpl.queryTreeMenus();
		if(list == null){
			return Ret.error();
		}
		return Ret.ok().put("data", list);
	}
	
	/**
	 * 新增文章
	 * @param userId
	 * @return
	 */
	@RequestMapping(value={"/insertText"})
	@ResponseBody
	public Ret insertText(Article article){
		article.setUserId(Convert.getlong(getUserId()));
		int index = articleServiceImpl.insertText(article);
		if(index != 0){
			return Ret.error();
		}
		return Ret.ok();
	}
	
	/**
	 * 修改文章
	 * @param userId
	 * @return
	 */
	@RequestMapping(value={"/updateArticle"})
	@ResponseBody
	public Ret updateArticle(Article article){
		int index = articleServiceImpl.updateArticle(article);
		if(index != 0){
			return Ret.error();
		}
		return Ret.ok();
	}
	
	/**
	 * 
	 * 加载文章列表
	 * @param user
	 * @param limit
	 * @param offset
	 * @return
	 */
    @PostMapping("/wenzhang")
    @ResponseBody
    public PageResultVo loadWenzhang(Article article, Integer limit, Integer offset){
        PageHelper.startPage(PageUtil.getPageNo(limit, offset),limit);
        List<Article> articleList = articleServiceImpl.selectArticles(article);
        PageInfo<Article> pages = new PageInfo<>(articleList);
        return ResultUtil.table(articleList,pages.getTotal());
    }
    
    /**
	 * 
	 * 加载文章列表
	 * @param user
	 * @param limit
	 * @param offset
	 * @return
	 */
    @GetMapping("/wenzhanglist")
    @ResponseBody
    public List<Article> Wenzhang(Article article){
    	return articleServiceImpl.selectArticles(article);
    }
    
	/**
	 * 
	 * 根据ArticleId查询并加载文章列表
	 * @param user
	 * @return articleId
	 */
    @PostMapping("/wenzhangByArticleId")
    @ResponseBody
    public Ret wenzhangByArticleId(Long articleId){
    	Article article = articleServiceImpl.queryArticleByArticleId(articleId);
    	if(article == null){
    		return Ret.error();
    	}
        return Ret.ok().put("data", article);
    }
    /**
	 * 
	 * 根据ArticleId查询并加载文章
	 * @param user
	 * @return articleId
	 */
    @PostMapping("/artByArticleId")
    @ResponseBody
    public Article artByArticleId(Long articleId){
    	return articleServiceImpl.queryArticleByArticleId(articleId);
    }
    
    /**
	 * 根据ArticleId删除文章
	 * @return articleId
	 */
    @PostMapping("/deleteArticle")
    @ResponseBody
    public Ret deleteArticle(Long articleId){
    	int index = articleServiceImpl.deleteArticle(articleId);
    	if(index != 0){
    		return Ret.error();
    	}
        return Ret.ok();
    }
}
